package io.zhoujie.modules.app.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.JsonObject;
import com.mchange.v2.lang.ObjectUtils;
import io.zhoujie.common.exception.SheyingException;
import io.zhoujie.common.utils.ObjectUtil;
import io.zhoujie.modules.admin.dao.AppUserDao;
import io.zhoujie.modules.admin.entity.AppUserEntity;
import io.zhoujie.modules.app.dao.AppReplyDao;
import io.zhoujie.modules.app.entity.AppReplyEntity;
import io.zhoujie.modules.app.service.AppReplyService;
import io.zhoujie.modules.app.utils.SensitiveWordChecker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zhoujie
 * @email 813685764@qq.com
 * @date 2024/3/15 23:04
 */
@Service("AppReplyService")
@Slf4j
public class  AppReplyServiceImpl extends ServiceImpl<AppReplyDao, AppReplyEntity> implements AppReplyService {
    @Autowired
    private AppReplyDao appReplyDao;
    @Autowired
    private AppUserDao appUserDao;

    @Override
    public List<AppReplyEntity> selectReplyList(AppReplyEntity appReplyEntity) {
        List<AppReplyEntity> appReplyEntities = appReplyDao.selectReplyList(appReplyEntity);
        return  appReplyEntities;
    }

    @Override
    public void insertReply(AppReplyEntity appReplyEntity) {
        if (!ObjectUtil.isEmpty(appReplyEntity)){
            log.info("插入提问信息：{}",appReplyEntity);
            AppUserEntity appUserEntity = appUserDao.selectById(appReplyEntity.getUserId());
            if (SensitiveWordChecker.containsSensitiveWord(appReplyEntity.getQuestContent())) {
                throw new SheyingException("存在敏感词汇");
            }
//            数据插入
            if(appUserEntity.getIntegral()>0){
                appReplyEntity.setImg(JSON.toJSONString(appReplyEntity.getImg()));
                appReplyEntity.setTime(new Date());
                appReplyEntity.setStatus("2");
                if (appReplyEntity.getQuestName()==null){
                    appReplyEntity.setQuestName("疑问解答");
                }
                baseMapper.insert(appReplyEntity);
                appUserEntity.setIntegral(appUserEntity.getIntegral()-1);
                appUserDao.updateById(appUserEntity);
            }
            else{
                throw new SheyingException("发布失败，积分不足");
            }
        }

    }

    @Override
    public List<JSONObject> listReply(Integer postId) {
        return appReplyDao.listReply(postId);
    }

    @Override
    public AppReplyEntity selectReplyByID(Integer id) {
        return appReplyDao.selectReplyByID(id);
    }

    @Override
    public int insertReplys(Map<String, Object> list) {
        Integer userId = (Integer) list.get("userId");
        String postId = (String) list.get("postId");
        String content = (String) list.get("content");
        String top = "1";
        if (SensitiveWordChecker.containsSensitiveWord(content)) {
            throw new SheyingException("存在敏感词汇");
        }else{
            //审核通过
            return appReplyDao.insertReplys(userId,postId,content,new Date(),top);
        }
    }

    @Override
    public int setTop(Map<String, Object> list) {
        Integer reply = (Integer) list.get("reply");
        Integer userId = (Integer) list.get("userId");
        AppUserEntity appUserEntity = appUserDao.selectById(userId);
        if (!ObjectUtil.isEmpty(appUserEntity)){
            appUserEntity.setUid(userId);
            //增加积分
            appUserEntity.setIntegral(appUserEntity.getIntegral()+1);
            appUserDao.updateById(appUserEntity);
        }
        return appReplyDao.setTop(reply);
    }
}
